package swingxrcp.framework.client.swingx;

import java.awt.Color;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Rectangle;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;
import javax.swing.border.BevelBorder;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;

import swingxrcp.framework.client.swingx.resources.Resources;

public class JXMessageDialog extends JXDialog {

	/** ********** Panel容器 ********** */
	private JPanel contentPanel = null;

	/** ************* 控件 ************ */
	private JLabel labIcon = null;
	private JPanel buttonPanel = null;
	private JEditorPane edtMessage = null;
	private JScrollPane slpMessage = null;

	/** ********** 按钮定义区 ********** */
	private JButton btnOK = null;
	private JButton btnClose = null;
	private JButton btnCancel = null;

	public JXMessageDialog() {
		super();

		// 初始化界面
		initialize();

        // 初始化脚本
		initScript();
	}

	public JXMessageDialog(boolean model) {
		super();

		setModal(model);

		// 初始化界面
		initialize();

        // 初始化脚本
		initScript();
	}

	/**
	 * @param owner
	 */
	public JXMessageDialog(Frame owner) {
		super(owner, "", true);

		// 初始化界面
		initialize();

        // 初始化脚本
		initScript();
	}

	/**
	 * @param owner
	 */
	public JXMessageDialog(Dialog owner) {
		super(owner, "", true);

		// 初始化界面
		initialize();

        // 初始化脚本
		initScript();
	}

	// TODO 初始化界面-开始====================================================
	/**
	 * This method initializes this
	 *
	 * @return void
	 */
	private void initialize() {
		this.setSize(350, 160);
		setTitle("提示信息");
		setResizable(false);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);

		setContentPane(getContentPanel());
	}

	/**
	 * This method initializes jContentPane
	 *
	 * @return javax.swing.JPanel
	 */
	private JPanel getContentPanel() {
		if (contentPanel == null) {
			labIcon = new JLabel();
			labIcon.setBounds(new Rectangle(0, 0, 80, 85));
			labIcon.setHorizontalAlignment(SwingConstants.CENTER);
			labIcon.setHorizontalTextPosition(SwingConstants.CENTER);
			labIcon.setText("");

			JSeparator separator = new JSeparator();
			separator.setBounds(0, 93, 340, 1);

			contentPanel = new JPanel();
			contentPanel.setLayout(null);
			contentPanel.add(labIcon, null);
			contentPanel.add(getButtonPanel(), null);
			contentPanel.add(separator);
			contentPanel.add(getSlpMessage(), null);
		}
		return contentPanel;
	}

	/**
	 * This method initializes buttonPanel
	 *
	 * @return javax.swing.JPanel
	 */
	private JPanel getButtonPanel() {
		if (buttonPanel == null) {
			buttonPanel = new JPanel();
			buttonPanel.setLayout(new FlowLayout());
			buttonPanel.setBounds(new Rectangle(0, 98, 340, 30));
			buttonPanel.add(getBtnOK(), null);
			buttonPanel.add(getBtnClose(), null);
			buttonPanel.add(getBtnCancel(), null);
		}
		return buttonPanel;
	}

	/**
	 * This method initializes edtMessage
	 *
	 * @return javax.swing.JEditorPane
	 */
	private JEditorPane getEdtMessage() {
		if (edtMessage == null) {
			edtMessage = new JEditorPane("text/html", "");
			edtMessage.setEditable(false);
			edtMessage.setBackground(getContentPanel().getBackground());
			edtMessage.setSelectionColor(new Color(249,224,137));
			edtMessage.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED,
					contentPanel.getBackground(), contentPanel.getBackground(),
					contentPanel.getBackground(), contentPanel.getBackground()));
			// 实现点击超链接
			final JEditorPane theEditorPane = edtMessage;
			edtMessage.addHyperlinkListener(new HyperlinkListener() {
				public void hyperlinkUpdate(final HyperlinkEvent hle) {
					try {
						if (hle.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
							theEditorPane.setPage(hle.getURL());
					} catch (IOException ioe) {
						ioe.printStackTrace(System.err);
					}
				}
			});
		}
		return edtMessage;
	}

	private JScrollPane getSlpMessage() {
		if(slpMessage == null) {
			slpMessage = new JScrollPane();
			slpMessage.setBounds(new Rectangle(80, 5, 260, 85));
			slpMessage.setViewportView(getEdtMessage());
			slpMessage.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
			slpMessage.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
			slpMessage.setHorizontalScrollBar(slpMessage.getHorizontalScrollBar());
			slpMessage.setViewport(slpMessage.getViewport());
			slpMessage.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED,
					contentPanel.getBackground(), contentPanel.getBackground(),
					contentPanel.getBackground(), contentPanel.getBackground()));
		}
		return slpMessage;
	}

	/**
	 * This method initializes btnOK
	 *
	 * @return javax.swing.JButton
	 */
	private JButton getBtnOK() {
		if (btnOK == null) {
			btnOK = new JButton();
			btnOK.setText("确定");
			btnOK.setPreferredSize(new Dimension(60, 22));
		}
		return btnOK;
	}
	/**
	 * This method initializes btnClose
	 *
	 * @return javax.swing.JButton
	 */
	private JButton getBtnClose() {
		if (btnClose == null) {
			btnClose = new JButton();
			btnClose.setText("关闭");
			btnClose.setPreferredSize(new Dimension(60, 22));
		}
		return btnClose;
	}
	/**
	 * This method initializes btnCancle
	 *
	 * @return javax.swing.JButton
	 */
	private JButton getBtnCancel() {
		if (btnCancel == null) {
			btnCancel = new JButton();
			btnCancel.setText("取消");
			btnCancel.setPreferredSize(new Dimension(60, 22));
		}
		return btnCancel;
	}

	// TODO 初始化界面-结束====================================================

	// TODO 显示对话框-开始====================================================
	public void showMessageDialog(String message) {
		showMessageDialog(message, INFORMATION_MESSAGE);
	}

	public int showQuestionMessageDialog(String message) {
		return showMessageDialog(message, QUESTION_MESSAGE);
	}

	public int showWarnMessageDialog(String message) {
		return showMessageDialog(message, WARN_MESSAGE);
	}

	public int showErrorMessageDialog(String message) {
		return showMessageDialog(message, ERROR_MESSAGE);
	}

	protected int showMessageDialog(String message, int type) {
		switch(type) {
			case INFORMATION_MESSAGE:{
				setTitle("信息提示");
				btnOK.setVisible(false);
				btnClose.setVisible(true);
				btnCancel.setVisible(false);
				labIcon.setIcon(new ImageIcon(getClass().getResource(Resources.ICON_INFORM)));

				edtMessage.setText(parseMessage(message,"black"));
				setDefaultButton(btnClose);
				break;
			}

			case QUESTION_MESSAGE:{
				setTitle("询问信息提示");
				btnOK.setVisible(true);
				btnClose.setVisible(false);
				btnCancel.setVisible(true);
				labIcon.setIcon(new ImageIcon(getClass().getResource(Resources.ICON_QUESTION)));

				edtMessage.setText(parseMessage(message,"black"));
				setDefaultButton(btnOK);
				break;
			}

			case WARN_MESSAGE:{
				setTitle("警告信息提示");
				btnOK.setVisible(false);
				btnClose.setVisible(true);
				btnCancel.setVisible(false);
				labIcon.setIcon(new ImageIcon(getClass().getResource(Resources.ICON_WARN)));

				edtMessage.setText(parseMessage(message,"blue"));
				setDefaultButton(btnClose);
				break;
			}

			case ERROR_MESSAGE:{
				setTitle("错误信息提示");
				btnOK.setVisible(true);
				btnClose.setVisible(false);
				btnCancel.setVisible(false);
				labIcon.setIcon(new ImageIcon(getClass().getResource(Resources.ICON_ERROR)));

				edtMessage.setText(parseMessage(message,"red"));
				setDefaultButton(btnOK);
				break;
			}
			default:{
				setTitle("信息提示");
				btnOK.setVisible(false);
				btnClose.setVisible(true);
				btnCancel.setVisible(false);
				labIcon.setIcon(new ImageIcon(getClass().getResource(Resources.ICON_INFORM)));

				edtMessage.setText(parseMessage(message,"black"));
				setDefaultButton(btnClose);
			}
		}
		setVisible(true);
		return option;
	}

	// TODO 显示对话框-结束====================================================
	/** *** 其他成员变量 *** */
	/** *** 询问对话框按扭返回值操作值 *** */
	public static final int YES_OPTION = 1;
	public static final int CANCLE_OPTION = 0;

	private int option = 0;

	// TODO 初始化脚本-开始====================================================
	public void initScript() {
		registerActions();
	}

	// TODO 初始化脚本-结束====================================================

	// ------------------ 分析信息 --------------------
	private String parseMessage(String message, String color) {
		Pattern p = Pattern.compile("\n");
        Matcher m = p.matcher(message);
        message = m.replaceAll("<br>");

		StringBuffer buffer = new StringBuffer();
		buffer.append("<font color=" + color + " size=3>");
		buffer.append(message);
		buffer.append("</font>");

		return buffer.toString();
	}
	// ------------------ 分析信息 --------------------

	// ---------------- 注册动作开始 ------------------
	public void registerActions() {
		registerAction(btnOK, "doOk");
		registerAction(btnClose, "doClose");
		registerAction(btnCancel, "doCancel");
	}

	private void setDefaultButton(JButton button) {
		getRootPane().setDefaultButton(button);
	}

	// ---------------- 注册动作结束 ------------------

	// ---------------- 响应动作开始 -----------------
	public void doOk() {
		dispose();
		option = YES_OPTION;
	}

	public void doClose() {
		dispose();
	}

	public void doCancel() {
		dispose();
		option = CANCLE_OPTION;
	}
	// ---------------- 响应动作结束 ------------------
	// ---------------- 执行动作开始 ------------------
	// ---------------- 执行动作结束 ------------------
	// ---------------- 外部调用方法开始 ---------------
	// ---------------- 外部调用方法结束 ---------------

	public static void main(String []args) {
		JXMessageDialog d = new JXMessageDialog();
		d.showMessageDialog("<Font color=red>保存错误!</font><br>原因:\n1.<font size=6 color=green>网络问题</font>;<br>2.数据不对."
						+ "\n<table border=3 cellspacing=2 cellpadding=1><tr><td>1</td></tr><tr><td>2</td></tr></table>");
	}
}  //  @jve:decl-index=0:visual-constraint="10,10"
